<ul id="error">
</ul>

<table>
    <tr>
        <th><a id="save" href="#">save</a></th>
        <th><?= $title ?></th>
    </tr>
    <tr>
        <td>Ссылка</td>
        <td><input type="text" name="href" value="<?= Arr::get($article, 'href') ?>"/></td>
    </tr>
    <tr>
        <td>Заголовок</td>
        <td><input type="text" name="html_title" value="<?= Arr::get($article, 'html_title') ?>"/></td>
    </tr>
    <tr>
        <td>Содержание</td>
        <td><textarea id="article_content" style="height:300px;" name="content"><?= Arr::get($article, 'content') ?></textarea></td>
    </tr>
    <tr>
        <td>Ключевые слова</td>
        <td><textarea name="html_keyword"><?= Arr::get($article, 'html_keyword') ?></textarea></td>
    </tr>
    <tr>
        <td>Описание</td>
        <td><textarea name="html_description"><?= Arr::get($article, 'html_description') ?></textarea></td>
    </tr>
    <input type="hidden" name="id" value="<?= Arr::get($article, 'id', '')?>">   
</table>


<script type="text/javascript">
    $(document).ready(function(){        
        var w = $('#article_content').width();
        var h = $('#article_content').height();
        
        function full_screen_editor(){
            if($('#article_content').hasClass('fullscreen')){
                $('#article_content').width(w);
                $('#article_content').height(h);
                $('#article_content').removeClass('fullscreen');             
            } else {
                $('#article_content').hide();
                $('#article_content').addClass('fullscreen')
                                     .width($('#content').innerWidth() - 22)
                                     .height($(window).height() - $('#menu').innerHeight() -22);                
                $('#article_content').show();

            }    
        }
        
        $('#article_content').bind('keydown', function(e){
            if(e.keyCode == 122){
                if(e.preventDefault) 
                    e.preventDefault();
                else
                    e.returnValue = false;
                
               full_screen_editor(); 
            }
        });
        
        $('#article_content').bind('dblclick', function(){
            full_screen_editor();
        })
        
        $('#save').bind('click', function(){
            $('#error').empty().slideUp();
            
            var data = {};
            $('#content table').find('input, textarea').each(function(){
                
                key = $(this).attr('name');
                
                value = $(this).val();
                data[key] = value;
            });
            
            $.ajax({'url':'/admin/article/save',
                    'type': 'POST',
                    'data': data,
                    'timeout':5000,
                    'success':function(k){
                        response = eval("(" + k + ")");
                        if(response.status == 'ok')
                            window.location.assign('/admin/article');
                        else {                            
                            var error = response.error;
                            $('#error').append('Ой, ошибочка вышла...<br/>');
                            for(var key in error){
                                $('#error').append('<li>' + error[key] + '</li>');             
                            }
                            $('#error').slideDown();
                        }   
                    },
                    'error':function(){
                        alert('Ошибка!');
                    }
            });

           return false;
        });
        
    });

</script>
